package com.example.demo.api.controller;

import com.example.demo.api.dto.StudentDTO;
import com.example.demo.app.service.CourseService;
import com.example.demo.app.service.GradesService;
import com.example.demo.app.service.StudentService;
import com.example.demo.config.SpringPropertiesConfig;
import com.example.demo.domain.entity.Student;
import com.example.demo.domain.vo.StudentGradesSimpleVo;
import io.swagger.models.auth.In;
import lombok.val;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/test")
public class TestController {
//    @Resource
//    private String first;
//    @Resource
//    private String second;
//    @Resource
//    private String third;
    @Resource
    private SpringPropertiesConfig springPropertiesConfig;

    @Resource
    private GradesService gradesService;

    @Resource
    private StudentService studentService;

    @Resource
    private CourseService courseService;

    @GetMapping("/getSpringProperties")
    @ResponseBody
    public String getSpringProperties(){
        return springPropertiesConfig.getFirst()+springPropertiesConfig.getSecond()+springPropertiesConfig.getThird();
    }

    @GetMapping("/toIndex")
    public ModelAndView toIndex(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("message","hello,world");
        modelAndView.addObject("studentGrades",studentService.queryStudentCourseGrade(null));
        modelAndView.addObject("courseList",courseService.queryCourseList());
        modelAndView.addObject("selectedStudent",new StudentGradesSimpleVo());
        modelAndView.setViewName("/index");
        return modelAndView;
    }

    @ResponseBody
    @GetMapping("/grade/check")
    public Integer check(Integer student,Integer course){
        val studentGradeDTOS = gradesService.queryGradesByCondition(student, null, course);

        return studentGradeDTOS.isEmpty() ?0:studentGradeDTOS.get(0).getScore();
    }

    @ResponseBody
    @PostMapping("/grade/update")
    public Integer update(@RequestBody StudentDTO studentDTO){
        return gradesService.updateScore(studentDTO.getStudentId(),studentDTO.getCourseId(),studentDTO.getScore());
    }

    @GetMapping("/student/check")
    public ModelAndView checkStudent(String sname){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("message","hello,world");
        modelAndView.addObject("studentGrades",studentService.queryStudentCourseGrade(sname));
        modelAndView.addObject("courseList",courseService.queryCourseList());
        modelAndView.addObject("selectedStudent",new StudentGradesSimpleVo());
        modelAndView.setViewName("/index");
        return modelAndView;
    }

    @ResponseBody
    @PostMapping("/student/delete")
    public Integer deleteStudent(@RequestBody StudentDTO studentDTO){
        return studentService.deleteStudentById(studentDTO.getStudentId());
    }

    @PostMapping("/student/insert")
    public Integer add(@RequestBody Student student){
        return studentService.addStudent(student);
    }

    @PostMapping("/student/update")
    public Integer save(@RequestBody Student student){
        return studentService.saveStudent(student);
    }

}
